//
//  RxAlamofireTest1ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxAlamofire
import RxSwift

class RxAlamofireTest1ViewController: UIViewController {
    
    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "42.1.将结果转为 JSON 对象"
        view.backgroundColor = .white
    }
    
    func test() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string:urlString)!
         
        //创建并发起请求
        request(.get, url)
            .data()
            .subscribe(onNext: {
                data in
                let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
                    as! [String: Any]
                print("--- 请求成功！返回的如下数据 ---")
                print(json!)
            }).disposed(by: disposeBag)
    }
    
    func test2() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string:urlString)!
         
        //创建并发起请求
        request(.get, url)
            .responseJSON()
            .subscribe(onNext: {
                dataResponse in
                let json = dataResponse.value as! [String: Any]
                print("--- 请求成功！返回的如下数据 ---")
                print(json)
            }).disposed(by: disposeBag)
    }
    
    func test3() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string:urlString)!
         
        //创建并发起请求
        requestJSON(.get, url)
            .subscribe(onNext: {
                response, data in
                let json = data as! [String: Any]
                print("--- 请求成功！返回的如下数据 ---")
                print(json)
            }).disposed(by: disposeBag)
    }

}
